﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Admin.Models;
using PagedList;
using Microsoft.Security.Application;

namespace Admin.Areas.Administration.Controllers
{
    public class HotelsController : AdminBaseController
    {
        private BookingDBEntities db = new BookingDBEntities();
       
        //
        // GET: /Administration/Hotels/

        public ActionResult Index(int? PageSize)
        {
            ViewBag.Folder = "Images";
            ViewBag.SubFolder = "Hotels";
            var tblhotels = db.TblHotels.Include(t => t.TblCity).Include(t => t.TblCountry).Include(t => t.TblUserProfile).Include(t=>t.TblTypeOfAccommodation);
            ViewBag.SubTitle = "danh_sach_khach_san";
            return View(tblhotels.ToList().ToPagedList(PageSize ?? 1,NumberOfPage));
        }

        //
        // GET: /Administration/Hotels/Details/5

        public ActionResult Details(int id = 0)
        {
            ViewBag.Folder = "Images";
            ViewBag.SubFolder = "Hotels";
            TblHotel tblhotel = db.TblHotels.Find(id);
            
            if (tblhotel == null)
            {
                return HttpNotFound();
            }
            return View(tblhotel);
        }

        //
        // GET: /Administration/Hotels/Create

        public ActionResult Create()
        {
            ViewBag.Folder = "Images";
            ViewBag.SubFolder = "Hotels";
            var city = from c in db.TblCities
                       where c.CountryID == 1
                       select c;

            ViewBag.CityID = new SelectList(city, "CityID", "CityName", city.First());
            //ViewBag.CityID = new SelectList(db.TblCities, "CityID", "CityName");
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName", 1);
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName");
            ViewBag.TypeOfHotel = new SelectList(db.TblTypeOfAccommodations, "TypeOfAccommodation", "TypeOfAccommodationName");
            return View();
        }

        //
        // POST: /Administration/Hotels/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(TblHotel tblhotel)
        {
            if (ModelState.IsValid)
            {
                db.TblHotels.Add(tblhotel);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            var city = from c in db.TblCities
                       where c.CountryID == tblhotel.CountryID
                       select c;

            ViewBag.CityID = new SelectList(city, "CityID", "CityName", tblhotel.CityID);
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName", tblhotel.CountryID);
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName", tblhotel.ContactNameID);
            ViewBag.TypeOfHotel = new SelectList(db.TblTypeOfAccommodations, "TypeOfAccommodation", "TypeOfAccommodationName", tblhotel.TypeOfAccommodation);
            return View(tblhotel);
        }

        //
        // GET: /Administration/Hotels/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ViewBag.Folder = "Images";
            ViewBag.SubFolder = "Hotels";
            TblHotel tblhotel = db.TblHotels.Find(id);
            if (tblhotel == null)
            {
                return HttpNotFound();
            }
            ViewBag.CityID = new SelectList(db.TblCities, "CityID", "CityName", tblhotel.CityID);
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName", tblhotel.CountryID);
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName", tblhotel.ContactNameID);
            return View(tblhotel);
        }

        //
        // POST: /Administration/Hotels/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(TblHotel tblhotel)
        {

            if (ModelState.IsValid)
            {
                tblhotel.Description = Sanitizer.GetSafeHtmlFragment(tblhotel.Description);
                db.Entry(tblhotel).State = EntityState.Modified;
               

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CityID = new SelectList(db.TblCities, "CityID", "CityName", tblhotel.CityID);
            ViewBag.CountryID = new SelectList(db.TblCountries, "CountryID", "CountryName", tblhotel.CountryID);
            ViewBag.ContactNameID = new SelectList(db.TblUserProfiles, "UserID", "UserName", tblhotel.ContactNameID);
            return View(tblhotel);
        }

        //
        // GET: /Administration/Hotels/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TblHotel tblhotel = db.TblHotels.Find(id);
            if (tblhotel == null)
            {
                return HttpNotFound();
            }
            return View(tblhotel);
        }

        //
        // POST: /Administration/Hotels/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            TblHotel tblhotel = db.TblHotels.Find(id);
            db.TblHotels.Remove(tblhotel);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        
    }
}